package com.spiro.bintest;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Arrays;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class Client {
    public static void main(String[] args) throws UnsupportedEncodingException {

        args = new String[] { "localhost", "9090" };

        if (args.length != 2) {
            System.out.println("Useage: com.spiro.ssl.JavaClient <server_ip> <server_port>");
            System.exit(0);
        }

        try {
            TTransport transport;
            transport = new TFramedTransport(new TSocket(args[0],
                    Integer.valueOf(args[1])));
            transport.open();

            TProtocol protocol = new TBinaryProtocol(transport);

            BinTest.Client bc = new BinTest.Client(protocol);

            ByteBuffer getB = bc.get();
            
            byte[] dst = new byte[getB.limit() - getB.position()];
            getB.get(dst);
            
            System.out.println("getBStr=" + Arrays.toString(dst));

            byte[] bs = { 120, 121 };
            ByteBuffer bfs = ByteBuffer.wrap(bs);
            System.out.println("Client put=>" + Arrays.toString(bfs.array())
                    + ", pos=" + bfs.position() + ", limit=" + bfs.limit());
            bc.put(bfs);

            transport.close();
        } catch (TException x) {
            x.printStackTrace();
        }

    }
}
